package com.marketingexpert.controller.statemgnt;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.marketingexpert.domain.ActivityRecord;
import com.marketingexpert.domain.User;
import com.marketingexpert.service.statreport.StatReportService;
import com.marketingexpert.service.usermgnt.UserService;

import java.io.PrintWriter;
import java.util.List;

public class ExportReportDataController implements Controller {

	private StatReportService statReportService;

	private UserService userService;

	private String dispatchUrl;

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		response.setContentType("application/x-json;charset=GBK"); 
		response.setHeader("Pragma", "No-cache"); 
		response.setHeader("Cache-Control", "no-cache"); 
		response.setHeader("Cache-Control", "no-store"); 
		response.setDateHeader("Expires", 0);  
		PrintWriter out = response.getWriter(); 	
		
		String pageNoStr = request.getParameter("pageNo");
		int pageNo = 0;		
		if(pageNoStr != null && !pageNoStr.equals("")) {
			pageNo = Integer.parseInt(pageNoStr);
		}		
		String parentUserId = (String) request.getSession().getAttribute("parentUserId");		
		User parentUser = getUserService().queryUserByUId(parentUserId);		
		List<ActivityRecord> activityRecourds = getStatReportService().queryActivityRecord(parentUser.getId(),pageNo * 5);
		JSONArray activityJsonArray = JSONArray.fromObject(activityRecourds);
		
		out.print(activityJsonArray);
		return null;
	}

	public StatReportService getStatReportService() {
		return statReportService;
	}

	public void setStatReportService(StatReportService statReportService) {
		this.statReportService = statReportService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getDispatchUrl() {
		return dispatchUrl;
	}

	public void setDispatchUrl(String dispatchUrl) {
		this.dispatchUrl = dispatchUrl;
	}
}
